using System.Net.WebSockets;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace CSharpPro._0930;

public class Demo0930
{
    public void TestPrint1()
    {
        Console.WriteLine("请输入三个整数");
        int []numbers = new int[3];
        int temp;
        for (int i = 0; i < 3; i++)
        {
            numbers[i] = int.Parse(Console.ReadLine());
        }

        for (int i = 0; i < numbers.Length; i++)
        {
            for (int j = i+1; j < numbers.Length; j++)
            {
                if (numbers[i] > numbers[j])
                {
                    temp =  numbers[i];
                    numbers[i] = numbers[j];    
                    numbers[j] = temp;
                }
            }
        }
        Console.WriteLine("把这三个数从小到大排序");
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.Write(numbers[i]+"  ");
        }
            
    }

    public void TestPrint2()
    {
        int sum = 0;
        List<int> numbers = new List<int>();
        for (int i = 1; i <101 ; i++)
        {
            if (i % 7 != 0 && i % 10 != 7 && i / 10 != 7)
            {
                numbers.Add(i);
            }
        }
        Console.WriteLine("输出1-100之间所有不是7的倍数和不包含7的数字");
        for (int i = 0; i < numbers.Count; i++)
        {
            Console.Write(numbers[i]+"  ");
            sum += numbers[i];
        }
        Console.WriteLine("对这样数求和");
        Console.WriteLine(sum);
    }

    public void TestPrint3()
    {
        Console.WriteLine("输出9*9乘法表");
        for (int i = 1; i < 10; i++)
        {
            for (int j = 1; j <=i; j++)
            {
                
                Console.Write(i+"*"+j+"="+i*j+"  ");
            }
            Console.WriteLine();
        }
    }


    public void TestPrint4()
    {
        Console.Write("1000以内的完数为：");
        List<int> numbers;
        int sum = 0;
        for (int i = 2; i < 1001; i++)
        {
            
            numbers = new List<int>();
            sum = 0;
            for (int j = 1; j < i; j++)
            {
                if (i % j == 0)
                {
                    numbers.Add(j);
                    sum += j;
                }   
            }

            if (sum == i)
            {
                Console.Write(i+"  ");
            }
        }
    }

    public void TestPrint5()
    {
        Console.WriteLine("输入一个整数");
        int add = int.Parse(Console.ReadLine());
        String str = add.ToString();
        int length = str.Length;
        Console.WriteLine("数字是"+length+"位");
        char[] substr = new char[length];
        for (int i = str.Length - 1, j = 0; i >= 0; i--, j++)
        {
            substr[j] = str[i];
        }
        Console.WriteLine("将这些数逆序");
        for (int i = 0; i < substr.Length; i++)
        {
            Console.Write(substr[i] + " ");
        }
    }

    public void TestPrint6()
    {
        Console.WriteLine("请输入菱形的行数");
        int add = int.Parse(Console.ReadLine());
        if (add % 2 != 0)
        {
            for (int i = 1; i <=add/2; i++)
            {
                for (int k = i; k < add / 2 + 1; k++)
                {
                    Console.Write(" ");
                }
                for (int j = 1; j <= 2*i-1; j++)
                {
                    
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            for (int i = add/2+1; i <=add; i++)
            {
                for (int k = 1; k <= i - (add / 2 + 1); k++)
                {
                    Console.Write(" ");
                }
                for (int j = 1; j <= 2*(add-(i-1))-1; j++)
                {
                    
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
        
    }

    public class TestClass
{
    
    private class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }

    public void TestPrint7()
    {
        List<User> users = new List<User>(); 
        bool isRunning = true;
        Console.WriteLine("=== 用户认证系统 ===");

        while (isRunning)
        {
            Console.WriteLine("\n请选择操作：");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("输入选项(1-3)：");
            string choice = Console.ReadLine()?.Trim() ?? "3";

            switch (choice)
            {
                case "1": // 登录流程
                    Console.Write("请输入用户名：");
                    string loginUser = Console.ReadLine()?.Trim() ?? "";
                    Console.Write("请输入密码：");
                    string loginPwd = Console.ReadLine() ?? "";

                    var user = users.Find(u => u.Username == loginUser && u.Password == loginPwd);
                    if (user != null)
                    {
                        Console.WriteLine("登录成功！");
                    }
                    else if (users.Exists(u => u.Username == loginUser))
                    {
                        Console.WriteLine("密码错误！");
                    }
                    else
                    {
                        Console.WriteLine("用户名不存在！");
                    }
                    break;

                case "2": // 注册流程
                    // 用户名验证
                    string username = "";
                    while (true)
                    {
                        Console.Write("请设置用户名(3-15位，仅含字母、数字、下划线)：");
                        username = Console.ReadLine()?.Trim() ?? "";
                        if (string.IsNullOrWhiteSpace(username) 
                            || username.Length < 3 
                            || username.Length > 15 
                            || !Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
                        {
                            Console.WriteLine("用户名格式不符合要求！");
                        }
                        else if (users.Exists(u => u.Username == username))
                        {
                            Console.WriteLine("该用户名已被注册！");
                        }
                        else
                        {
                            break;
                        }
                    }

                    // 密码验证
                    string password = "";
                    while (true)
                    {
                        Console.Write("请设置密码(6-20位，需包含大小写字母和数字)：");
                        password = Console.ReadLine() ?? "";
                        if (password.Length < 6 
                            || password.Length > 20 
                            || !Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
                        {
                            Console.WriteLine("密码格式不符合要求！");
                        }
                        else
                        {
                            Console.Write("请确认密码：");
                            string confirmPwd = Console.ReadLine() ?? "";
                            if (password == confirmPwd)
                            {
                                break;
                            }
                            else
                            {
                                Console.WriteLine("两次输入的密码不一致！");
                            }
                        }
                    }

                    users.Add(new User { Username = username, Password = password });
                    Console.WriteLine("注册成功！");
                    break;

                case "3": // 退出系统
                    isRunning = false;
                    Console.WriteLine("系统已退出，再见！");
                    break;

                default:
                    Console.WriteLine("无效的选项，请重新输入！");
                    break;
            }
        }
    }

   
   
}
   
}